$(function () {

    /*判断邮箱格式是否正确或者邮箱是否存在*/
    var emailStyle = true;
    var mail = $("#email");
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    $("#email").blur(function () {
        if (mail.val() == ""){
            $("#emailMess").html("邮箱未填写");
            return;
        }
        if (!myreg.test(mail.val())){
            $("#emailMess").html("邮箱格式不正确");
            emailStyle = false;
            return;
        }
    });

    /*判断用户和邮箱地址是否匹配*/
    $("#sendCaptcha").click(function () {
        $("#sendMess").html("邮件发送中......");
        $.ajax({
            type: "GET",
            url: "/sendEmail",
            data: {email: mail.val()},
            dataType: "json",
            success: function(data){
                // 显示邮箱是否存在的信息
                if (!data.mailExists){
                    if (emailStyle){
                        $("#emailMess").html("该邮箱不存在");
                    }
                } else {
                    $("#emailMess").html("");
                }
                // 显示邮件是否发送成功
                if (!data.status){
                    $("#sendMess").html("邮件发送失败");
                } else {
                    $("#sendMess").html("邮件发送成功");
                }
            }
        })
    });

    /* 验证验证码 */
    $("#submit").click(function () {
        $.ajax({
            type: "GET",
            url: "/verification",
            data: {captcha: $("#captcha").val()},
            dataType: "json",
            //timeout: 500,
            success: function (data) {
                if (!data.correct){
                    $("#captchaMess").html("验证码输入错误，验证失败！");
                } else {
                    window.location.href = "/resetPassword";
                }
            }
        })
    })
})